//    Copyright (C) Kherty.  All rights reserved.
using System.Windows.Input;
using System.Windows;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    class GridConditionalFormatting
    {
        internal static void EnableCommands(UIElement g)
        {
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ApplyRule, OnApplyRule, WorksheetHelper.OnHasSelection));
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ClearTableRules, OnClearTableRules, OnHasTable));
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ClearSelectionRules, OnClearSelectionRules, WorksheetHelper.OnHasSelection));
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ClearSheetRules, OnClearSheetRules, WorksheetHelper.OnHasSelection));
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.CreateAndApplyRule, OnCreateAndApplyRule, WorksheetHelper.OnHasSelection));

        }

        private static void OnCreateAndApplyRule(object sender, ExecutedRoutedEventArgs e)
        {
            var rule = (StandardConditionalFormattingRule) e.Parameter;
            rule = (StandardConditionalFormattingRule) rule.Clone();
            rule.Formula1 = "0";
            if (PropertyHelper.ShowPropertiesNonHostedFor(sender as FrameworkElement, UIStringTable.NewFormattingRule, rule, new[]{new FormattingRulePropertiesResources()}))
            {
                Worksheet worksheet = GetWorksheet(sender);
                Range selection = worksheet.Selection;
                worksheet.ApplyFormattingRule(rule, selection);
            }
            e.Handled = true;
        }

        private static void OnHasTable(object sender, CanExecuteRoutedEventArgs e)
        {
            Worksheet worksheet = GetWorksheet(sender);
            e.CanExecute = worksheet.ActiveCell.Table != null;
            e.Handled = true;
        }

        private static void OnClearSheetRules(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet worksheet = GetWorksheet(sender);
            worksheet.ClearRules();
            e.Handled = true;
        }
        private static void OnClearTableRules(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet worksheet = GetWorksheet(sender);
            worksheet.ClearRules(worksheet.ActiveCell.Table.Range);
            e.Handled = true;
        }
        private static void OnClearSelectionRules(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet worksheet = GetWorksheet(sender);
            worksheet.ClearRules(worksheet.Selection);
            e.Handled = true;
        }

        private static void OnApplyRule(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet worksheet = GetWorksheet(sender);
            Range selection = worksheet.Selection;
            worksheet.ApplyFormattingRule((ConditionalFormattingRule) e.Parameter, selection);
            e.Handled = true;
        }
        private static Worksheet GetWorksheet(object sender)
        {
            return WorksheetHelper.GetWorksheet(sender);
        }


    }
}
